package com.ctbu;

import com.ctbu.configure.NacosSameClusterConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@SpringBootApplication
//开启自定义负载均衡设置
@LoadBalancerClients(defaultConfiguration = NacosSameClusterConfiguration.class)
//指定使用自定义负载均衡的实例
//@LoadBalancerClient(name = "bank-service",configuration = {NacosSameClusterConfiguration.class})
//开启feign支持
@EnableFeignClients
public class ProductApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductApplication.class);
    }

    //定义并引入一个restTemplate类
    @Bean
    @LoadBalanced //启用负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    /**
     * 跨域过滤器
     * @return
     */
/*   @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }*/

}
